﻿using OvenParameterForm.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms.VisualStyles;

namespace OvenParameterForm.Technics
{
    public class 抽真空 : TechnicsBaseClass
    {
        public string 抽气设定 { get; set; }
        public string 工艺时间 { get; set; }
        private double 烘箱真空计读数 = 0.0;
        private int 当前计时 = 0;
        private int 总时间;
        private bool 达到抽气;
        private bool 正在抽气;
        public 抽真空(string[] strings):base(strings)
        {
            抽气设定 = strings[1];
            工艺时间 = strings[2];
        }
        public 抽真空(string name):base(name) 
        {
        }

        public override void Run()
        {
            当前计时 = 0;
            总时间 = Convert.ToInt32(工艺时间) * 60;
            PLC.WriteData(ovenPLCaddress.小补气地址, "0");
            PLC.WriteData(ovenPLCaddress.抽气地址, "0");
            base.Run();
        }
        protected override void updatetime(object state)
        {
            烘箱真空计读数 = Convert.ToDouble(PLC.InovanceAM400PLCRead(ovenPLCaddress.真空计整数地址,16) + "." + PLC.InovanceAM400PLCRead(ovenPLCaddress.真空计小数地址,16)) * Math.Pow(10, Convert.ToInt16(PLC.InovanceAM400PLCRead(ovenPLCaddress.真空计指数地址,16)));
            正在抽气 = PLC.ReadData(ovenPLCaddress.抽气地址).Equals("1");
            达到抽气 = 烘箱真空计读数 <= Convert.ToDouble(抽气设定);
            switch (PublicFunc.BoolArrayGetValue(new bool[] { 达到抽气, 正在抽气 }))
            {
                case 0:
                    PLC.WriteData(ovenPLCaddress.抽气地址, "1");
                    sendStatus($"开始抽气...计时：{当前计时}/{总时间}");
                    break;
                case 1:
                    sendStatus($"正在抽气....计时：{当前计时}/{总时间}");
                    break;
                case 2:
                    sendStatus($"抽气真空度已达标...计时：{当前计时}/{总时间}");
                    break;
                case 3:
                    PLC.WriteData(ovenPLCaddress.抽气地址, "0");
                    sendStatus($"抽气真空度刚达标，抽气停止....计时：{当前计时}/{总时间}");
                    break;
            }

            当前计时++;
        }

        protected override bool Complete()
        {
            if (当前计时 == 总时间)
            {
                if (!达到抽气) PLC.WriteData(ovenPLCaddress.没抽到报警地址, "1");
                PLC.WriteData(ovenPLCaddress.抽气地址, "0");
                return true;
            }
            else
            {
                return false;
            }

        }

        protected override void ClearVariable()
        {
            PLC.WriteData(ovenPLCaddress.抽气地址, "0");
        }
    }
}
